package cn.chinaflame.pagodas.web;

import java.util.List;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.google.common.base.Strings;

import cn.chinaflame.pagodas.Result.Result;
import cn.chinaflame.pagodas.annotation.ApiDoc;
import cn.chinaflame.pagodas.dto.PaProductPositionDto;
import cn.chinaflame.pagodas.enums.CodeEnum;
import cn.chinaflame.pagodas.service.PaProductPointService;
import cn.chinaflame.pagodas.service.PaProductPositionService;
import cn.chinaflame.pagodas.utils.ParamPreconditions;

/**
 * 商品位置接口
 * @author  created by boming
 * @date    2018年1月5日 上午9:45:37
 */
@RestController
@Transactional(rollbackFor = Exception.class)
@RequestMapping(value = "/openapi/v1/pagodas/product/position/")
public class OpenPaProductPositionAPI {

	private static final Logger log = LoggerFactory.getLogger(OpenPaProductPositionAPI.class);

	@Autowired
	private PaProductPositionService ywProductPositionService;
	/**
	 * 查询商品位置列表
	 * @param pageIndex 页码
	 * @param pageSize 页数
	 * @param id 商品id
	 * @param name 商品名称
	 * @return
	 */
	@GetMapping("query/list")
	@ApiDoc(Result.class)
	public Object queryList(Integer pageIndex, Integer pageSize, String id, String name, String areaId, String productId) {
		log.info("查询商品位置列表id:{},name:{},areaId:{},productId:{}", id, name, areaId, productId);
		String agentId = null;
		return ywProductPositionService.queryList1(pageIndex, pageSize, agentId, name, areaId, productId);
	}
	/**
	 * 查询商品位置
	 * @param id 商品id
	 * @return
	 */
	@GetMapping("query")
	@ApiDoc(Result.class)
	public Object query(String id) {
		log.info("查询单个商品位置id:{}", id);
		ParamPreconditions.checkNotEmpty(id, CodeEnum.FAIL.getCode(), "id不能为空");
		List<PaProductPositionDto> dtoList = ywProductPositionService.queryList(null, null, id, null, null).getData();
		ParamPreconditions.checkArgument(dtoList != null && dtoList.size()>0 && dtoList.get(0) != null, CodeEnum.FAIL.getCode(), "未找到商品位置信息");
		return Result.create(dtoList.get(0));
	}
	
	
	/**
	 * 添加单个商品位置
	 * @param name		位置名称
	 * @param areaId	区域id
	 * @return
	 */
	@PostMapping(value = "add")
	@ApiDoc(Result.class)
	public Object add(@RequestBody Map<String, String> map) {
		String name = map.get("name");
		String areaId = map.get("areaId");
		String cameraIp = map.get("cameraIp");
		Integer rows = !Strings.isNullOrEmpty(map.get("rows")) ? Integer.parseInt(map.get("rows")) : null;
		Integer cols = !Strings.isNullOrEmpty(map.get("cols")) ? Integer.parseInt(map.get("cols")) : null;
		log.info("添加单个商品位置name:{}, areaId:{}, rows:{}, cols:{}", name, areaId, cameraIp, rows, cols);
		PaProductPositionDto dto = ywProductPositionService.add1(name, areaId, cameraIp, rows, cols);
		return Result.create(dto);
	}
	/**
	 * 更新单个商品位置
	 * @param id 商品id
	 * @param name 商品名称
	 * @param cameraIp 摄像id（暂未使用）
	 * @param rows
	 * @param cols
	 * @return
	 */
	@PutMapping("update")
	@ApiDoc(Result.class)
	public Object update(String id, String name, String cameraIp, String rows, String cols) {
		log.info("更新单个商品位置id:{},name:{},rows:{},cols:{}", id, name, rows, cols);
		PaProductPositionDto dto = ywProductPositionService.update1(id, name,cameraIp, rows, cols);
		return Result.create(dto);
	}
	/**
	 * 删除商品位置信息
	 * @param id  商品id
	 * @return
	 */
	@DeleteMapping("delete/{id}")
	@ApiDoc(Result.class)
	public Object delete(@PathVariable("id")String id) {
		log.info("删除单个商品位置id:{}", id);
		Boolean b = ywProductPositionService.delete1(id);
		return Result.create(b);
	}
	/**
	 * 寻灯配置接口
	 * @param type			1-按中间行寻灯；2-左至右；3-右至左
	 * @param direction		中间寻灯时：left-左至右；right-右至左；seq-顺序
	 * @return
	 */
	@DeleteMapping("searchLightConfig")
	@ApiDoc(Result.class)
	public Object findLightConfig(Integer type, String direction){
		log.info("寻灯配置接口type:{},direction:{}",type, direction);
		PaProductPointService.type = type;
		PaProductPointService.direction = direction;
		return Result.create("成功");
	}
}
